package com.zsrt.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.zsrt.Utils.CheckCodeUtil;

public class CodeServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String checkCode=CheckCodeUtil.generateCode();
		req.getSession().setAttribute("code",checkCode);
		BufferedImage photo=CheckCodeUtil.getPhotoCode(checkCode);
		res.setContentType("image/jpeg");
		OutputStream out=res.getOutputStream();
		JPEGCodec.createJPEGEncoder(out).encode(photo);
		out.flush();
	}

}
